<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'mo:route:mesOrderRoute:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.scOrder.code }}
        </a>
      </template>
      <!-- 勾选 -->
      <!-- @change="handleSuccess" -->
      <template #checkBox="{ record, column }">
        <a-checkbox :checked="record[column.dataIndex] == '1' ? true : false" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoRouteMesOrderRouteList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { mesOrderRouteDelete, mesOrderRouteListData } from '/@/api/mes/mo/route/mesOrderRoute';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('mo.route.mesOrderRoute');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('订单工艺路线管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('订单号'),
        field: 'scOrder.code',
        component: 'Input',
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('工序名称'),
        field: 'basOper.name',
        component: 'Input',
      },
      {
        label: t('计价方式'),
        field: 'costType',
        component: 'Select',
        componentProps: {
          dictType: 'mes_price_mode',
          allowClear: true,
        },
      },
      {
        label: t('技能标准'),
        field: 'techLevel',
        component: 'Select',
        componentProps: {
          dictType: 'mes_tech_level',
          allowClear: true,
        },
      },
      {
        label: t('技能控制'),
        field: 'techCtrl',
        component: 'Select',
        componentProps: {
          dictType: 'mes_tech_ctrl',
          allowClear: true,
        },
      },
      {
        label: t('报工方式'),
        field: 'reportType',
        component: 'Select',
        componentProps: {
          dictType: 'mes_report_type',
          allowClear: true,
        },
      },
      {
        label: t('关键工序'),
        field: 'bkey',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
      {
        label: t('是否委外'),
        field: 'bout',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
      {
        label: t('是否报工'),
        field: 'breport',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
      {
        label: t('是否检验'),
        field: 'bcheck',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
      {
        label: t('是否上机'),
        field: 'bmachine',
        component: 'CheckboxGroup',
        componentProps: {
          options: [{ label: '', value: '1' }],
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'scOrder.code',
      key: 'scOrder.code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('行号'),
      dataIndex: 'scOrder.sortNum',
      key: 'a.scOrder.sortNum',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('工序编码'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工序名称'),
      dataIndex: 'basOper.name',
      key: 'oper.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('加工顺序'),
      dataIndex: 'sortNum',
      key: 'a.sort_num',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('关键工序'),
      dataIndex: 'bkey',
      key: 'a.bkey',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('计价方式'),
      dataIndex: 'costType',
      key: 'a.cost_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_price_mode',
    },
    {
      title: t('是否委外'),
      dataIndex: 'bout',
      key: 'a.bout',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否报工'),
      dataIndex: 'breport',
      key: 'a.breport',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否检验'),
      dataIndex: 'bcheck',
      key: 'a.bcheck',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否上机'),
      dataIndex: 'bmachine',
      key: 'a.bmachine',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('技能标准'),
      dataIndex: 'techLevel',
      key: 'a.tech_level',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_tech_level',
    },
    {
      title: t('技能控制'),
      dataIndex: 'techCtrl',
      key: 'a.tech_ctrl',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_tech_ctrl',
    },
    {
      title: t('报工方式'),
      dataIndex: 'reportType',
      key: 'a.report_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_report_type',
    },
    {
      title: t('计件单价'),
      dataIndex: 'iprice',
      key: 'a.iprice',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('工序说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 150,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑订单工艺路线'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'mo:route:mesOrderRoute:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除订单工艺路线'),
        popConfirm: {
          title: t('是否确认删除订单工艺路线'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'mo:route:mesOrderRoute:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: mesOrderRouteListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await mesOrderRouteDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
